package com.soft.sitegenerator.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.util.Collection;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;

public class HibernateDAOImpl extends HibernateDaoSupport implements HibernateDAO{
    protected DataBaseComponent component;
    protected DataSource dataSource;
    protected final Logger logger=Logger.getLogger(HibernateDAOImpl.class);

    public HibernateDAOImpl(Connection conn){}
    public HibernateDAOImpl(){}

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public DataBaseComponent getComponent() {
        return component;
    }

    public void setComponent(DataBaseComponent component) {
        this.component = component;
    }
    public void load(Object object, Serializable clave)throws ObjectNotFoundException {
        try{
            long inicio=System.currentTimeMillis();
            getHibernateTemplate().load(object,clave);
            long fin=System.currentTimeMillis();
            System.out.println("Tiempo ejecucion:"+(fin-inicio));
        }catch(Exception e){
            throw new ObjectNotFoundException(e.getMessage());
        }
    }
    public void exist(Object object, Serializable clave)throws ObjectNotFoundException {
        try{
            long inicio=System.currentTimeMillis();
            getHibernateTemplate().load(object,clave);
            long fin=System.currentTimeMillis();
            System.out.println("Tiempo ejecucion:"+(fin-inicio));
        }catch(Exception e){
            throw new ObjectNotFoundException(e.getMessage());
        }
    }
    public Serializable save(Object object){
        long inicio=System.currentTimeMillis();
        Serializable ret= getHibernateTemplate().save(object);
        long fin=System.currentTimeMillis();
        System.out.println("Tiempo ejecucion:"+(fin-inicio));
        getHibernateTemplate().flush();
        return ret;
    }
    public void saveOrUpdate(Object object){
        long inicio=System.currentTimeMillis();
        getHibernateTemplate().saveOrUpdate(object);
        long fin=System.currentTimeMillis();
        System.out.println("Tiempo ejecucion:"+(fin-inicio));
        getHibernateTemplate().flush();
    }
    public void update(Object object){
        long inicio=System.currentTimeMillis();
        getHibernateTemplate().update(object);
        long fin=System.currentTimeMillis();
        System.out.println("Tiempo ejecucion:"+(fin-inicio));
        getHibernateTemplate().flush();
    }
    public void delete(Object object){
        long inicio=System.currentTimeMillis();
        getHibernateTemplate().delete(object);
        long fin=System.currentTimeMillis();
        System.out.println("Tiempo ejecucion:"+(fin-inicio));
        getHibernateTemplate().flush();
    }
    public Collection find(String cadenaHQL,Object criterio){
        long inicio=System.currentTimeMillis();
        Collection ret=getHibernateTemplate().find(cadenaHQL,criterio);
        long fin=System.currentTimeMillis();
        System.out.println("Tiempo ejecucion:"+(fin-inicio));
        return ret;
    }
    public Collection find(String cadenaHQL,Object []criterio){
        long inicio=System.currentTimeMillis();
        Collection ret=getHibernateTemplate().find(cadenaHQL,criterio);
        long fin=System.currentTimeMillis();
        System.out.println("Tiempo ejecucion:"+(fin-inicio));
        return ret;
    }
    public Collection find(String cadenaHQL){
        long inicio=System.currentTimeMillis();
        Collection ret=getHibernateTemplate().find(cadenaHQL);
        long fin=System.currentTimeMillis();
        System.out.println("Tiempo ejecucion:"+(fin-inicio));
        return ret;
    }
}
